package com.gs.zsmanage.service.impl;

import com.gs.zsmanage.service.PdfFileService;
import com.gs.zsmanage.utils.Consts;
import com.gs.zsmanage.utils.FileSortUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class PdfFileServiceImpl implements PdfFileService {

    /*
     * 重要天气公报
     * 10.138.1.68 [qxdb].[dbo].[jcfw]
     * */

    @Override
    public List<String> listImportantFiles(String startTime, String endTime) {
        long sTime = 0;
        long eTime = 0;
        if (startTime != null && endTime != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                Date sdate = sdf.parse(startTime + "000000");
                Date edate = sdf.parse(endTime + "240000");
                Calendar c = Calendar.getInstance();
                c.setTime(sdate);
                sTime = c.getTimeInMillis();
                c.setTime(edate);
                eTime = c.getTimeInMillis();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        File file = new File(Consts.IMPORTANTREPORT_LOCAL_PATH);
        if (!file.exists()) return null;
        List<String> aimFiles = new ArrayList<>();
        File[] files = file.listFiles();
        FileSortUtil.OrderByDate(files);
        if (files != null && files.length != 0) {
            if (sTime != 0 && eTime != 0) {
                for (File temp : files) {
                    if (temp.lastModified() >= sTime && temp.lastModified() <= eTime) {
                        aimFiles.add(temp.getName());
                    }
                }
            } else {
                for (File temp : files) {
                    aimFiles.add(temp.getName());
                }
            }
        }
        return aimFiles;
    }

    @Override
    public List<String> listWeeklyFiles(String startTime, String endTime) {
        long sTime = 0;
        long eTime = 0;
        if (startTime != null && endTime != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                Date sdate = sdf.parse(startTime + "000000");
                Date edate = sdf.parse(endTime + "240000");
                Calendar c = Calendar.getInstance();
                c.setTime(sdate);
                sTime = c.getTimeInMillis();
                c.setTime(edate);
                eTime = c.getTimeInMillis();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        File file = new File(Consts.WEEKLYREPORT_LOCAL_PATH);
        if (!file.exists()) return null;
        List<String> aimFiles = new ArrayList<>();
        File[] files = file.listFiles();
        FileSortUtil.OrderByDate(files);
        if (files != null && files.length != 0) {
            if (sTime != 0 && eTime != 0) {
                for (File temp : files) {
                    if (temp.lastModified() >= sTime && temp.lastModified() <= eTime) {
                        aimFiles.add(temp.getName());
                    }
                }
            } else {
                for (File temp : files) {
                    aimFiles.add(temp.getName());
                }
            }
        }
        return aimFiles;
    }

    @Override
    public List<String> listPPTFiles(String year) {
        List<String> aimFiles = new ArrayList<>();
        if (year != null) {
            File file = new File(Consts.PPT_LOCAL_PATH + year + "/");
            if (!file.exists()) return null;
            File[] files = file.listFiles();
            FileSortUtil.OrderByDate(files);
            if (files != null && files.length != 0) {
                for (File temp : files) {
                    aimFiles.add(temp.getName());
                }
            }
        }
        return aimFiles;
    }

}
